package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.service.AdminBrandService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminBrandServiceImpl implements AdminBrandService {

    @Autowired
    MarketBrandMapper brandMapper;


    @Override
    public List<MarketBrand> list(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if (id!=null) {
            criteria.andIdEqualTo(id);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        marketBrandExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(marketBrandExample);
        return marketBrands;

    }

    @Override
    @Transactional
    public MarketBrand create(String name, String desc, String picUrl, BigDecimal floorPrice) {
        MarketBrand marketBrand = new MarketBrand();

        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setPicUrl(picUrl);
        marketBrand.setFloorPrice(floorPrice);
        if (StringUtils.isEmpty(picUrl)) {
            marketBrand.setPicUrl("");
        }
        marketBrand.setAddTime(LocalDateTime.now());
        marketBrand.setUpdateTime(LocalDateTime.now());
        brandMapper.insert(marketBrand);
        return marketBrand;
    }

    @Override
    @Transactional
    public int delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
        return 0;
    }

    @Override
    @Transactional
    public int update(MarketBrand marketBrand) {
//        MarketBrand marketBrand = new MarketBrand();
//        marketBrand.setPicUrl(picUrl);
//        BigDecimal floorPriceDecimal = new BigDecimal(floorPrice);
//        marketBrand.setFloorPrice(floorPriceDecimal);
//        marketBrand.setDesc(desc);
//        marketBrand.setAddTime(addTime);
//        marketBrand.setUpdateTime(updateTime);
//        marketBrand.setDeleted(deleted);
        int i = brandMapper.updateByPrimaryKey(marketBrand);
        return i;
    }

}
